from app.extensions import db
from app.modules.common.utils import JsonEncoder


class Role(db.Model, JsonEncoder):
    __tablename__ = "role"
    role_id = db.Column(db.Integer, primary_key = True)
    role_name = db.Column(db.String, nullable = False, unique = True)
    desc = db.Column(db.String)

    def __init__(self, role_name, desc):
        self.role_name = role_name
        self.desc = desc

    def __repr__(self):
        return (
            "<{class_name}("
            "role_id={self.role_id}, "
            "role_name=\"{self.role_name}\", "
            "desc=\"{self.desc}\", "
            ")>".format(
                class_name = self.__class__.__name__,
                self = self
            )
        )

    def to_dict(self):
        return {
            'role_id': self.role_id,
            'role_name': self.role_name,
            'desc': self.desc,
        }


class RoleMenu(db.Model, JsonEncoder):
    __tablename__ = "role_menu"
    role_id = db.Column(db.Integer, primary_key = True)
    menu_id = db.Column(db.String, primary_key = True)

    def __init__(self, role_id, menu_id):
        self.role_id = role_id
        self.menu_id = menu_id

    def to_dict(self):
        return {
            'role_id': self.role_id,
            'menu_id': self.menu_id,
        }
